package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.SimpleTimeZone;
//import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class SetTop extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       try {
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          DBCollection table = db.getCollection("top");
          //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");  
          //sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          String r = request.getParameter("del");
          if(r != null && r.length()!=0) {
             String s=request.getParameter("server");
             String g=request.getParameter("gname");
             table.remove(new BasicDBObject().append("start", r).append("server",s).append("groupname",g));
             response.sendRedirect("./SetTop?modify="+r);
             return;
          }
          String d = request.getParameter("modify");
          if(d == null || d.length()==0) {
             out.println("going to setting home");
             response.sendRedirect("./Sethome");
             return;
          }
          String server, groupname;
          int statsday, count;
          int i=0;
          for(i=1;i <= 5;i++) {
             server = request.getParameter("server"+i); 
             if(server != null && server.length() != 0) {
                groupname = request.getParameter("groupname"+i);
                statsday = Integer.valueOf(request.getParameter("statsday"+i));
                count = Integer.valueOf(request.getParameter("count"+i));
                BasicDBObject doc = new BasicDBObject("start",d)
                   .append("server",server)
                   .append("groupname",groupname)
                   .append("statsday",statsday)
                   .append("count",count);
                table.insert(doc);
             }
          }

          out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body>");
          out.println("<FORM action='./SetTop'><input type='hidden' name='modify' value='"+d+"'>");
          out.println("<table border='1' style='width:300px'><tr><th colspan=5>FOXIT Setting Tool(Top 廣告組)</th></tr>");
          out.println("<tr><th>server</th><th>組名</th><th>統計天數</th><th>TOP數</th><th></th></tr>"); 
          BasicDBObject query = new BasicDBObject("start", d);
          DBCursor cursor = table.find(query);
          while (cursor.hasNext()) {
             Top s = (Top) JSON.parseObject(cursor.next().toString(),Top.class);
             out.println("<tr><td>"+s.getServer()+"</td><td>"+s.getGroupname()+"</td><td>"+s.getStatsday()+"</td><td>"+s.getCount()+"</td><td><a href='./SetTop?del="+d+"&server="+s.getServer()+"&gname="+s.getGroupname()+"'>刪除</a></td>");
          }
          cursor.close();
          for(i=1;i <= 5;i++) {
             out.println("<tr><td><input type='text' name='server"+i+"'></td>");
             out.println("<td><input type='text' name='groupname"+i+"'></td>");
             out.println("<td><input type='text' name='statsday"+i+"'></td>");
             out.println("<td><input type='text' name='count"+i+"'></td>");
             out.println("<td></td></tr>"); 
          }
          out.println("<td colspan=3><input type='submit' value='submit'></td><td colspan=2><a href='./Slist?modify="+d+"'>回setting主選單</a></td></tr></table></form></body></html>");
       //} catch (UnknownHostException e) {
       //} catch (MongoException e) {
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }

    }
} 
